package com.my.algorithmlearning.leetcode;

import org.springframework.util.Assert;

import java.util.Arrays;

/**
 * @ClassName Medium_75_SortColors
 * @Description Medium_75_SortColors
 * @Author wanghaiwei
 * @Date 2023/3/8 10:27
 */
public class Medium_75_SortColors {
    public void sortColors(int[] nums) {
        if (nums == null || nums.length < 2) {
            return;
        }
        int n0 = 0;
        int n1 = 0;
        int n2 = 0;
        for (int i = 0; i < nums.length; i++) {
            if (nums[i] == 0) {
                n0++;
            }
            if (nums[i] == 1) {
                n1++;
            }
            if (nums[i] == 2) {
                n2++;
            }
        }
        for (int i = 0; i < n0; i++) {
            nums[i] = 0;
        }
        for (int i = n0; i < n0 + n1; i++) {
            nums[i] = 1;
        }
        for (int i = n0 + n1; i < n0 + n1 + n2; i++) {
            nums[i] = 2;
        }
    }

    public static void main(String[] args) {
        Medium_75_SortColors solution = new Medium_75_SortColors();
        int[] input = new int[]{2, 0, 2, 1, 1, 0};
        solution.sortColors(input);
        Assert.isTrue("[0, 0, 1, 1, 2, 2]".equals(Arrays.toString(input)), "nums = [2,0,2,1,1,0]计算失败，预期：[0,0,1,1,2,2]，实际：" + Arrays.toString(input));
    }
}
